﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Linq;

namespace PasrseCode
{
    public struct ResourceEntryInfo
    {
        public string ClassID;
        public string Translation;
        public string Description;
        public string ModDate;
    }

    public struct ResourceInfo
    {
        public string Name;
        public string Title;
        public string PluralTitle;
        public string Description;        
    }

    public class ResourceClassInfo
    {
        public ResourceInfo Info;
        public List<ResourceEntryInfo> Entries;
    }

    public interface IResourceReader
    {
        ResourceClassInfo Read();
    }

    public class XmlResourceReader : IResourceReader
    {
        private string fileName;

        public XmlResourceReader(string resourceName)
        {
            this.fileName = resourceName;
        }

        #region IResourceReader Members

        public ResourceClassInfo Read()
        {
            XDocument doc = XDocument.Load(this.fileName);
            var root = doc.Root;
            var res = new ResourceClassInfo();
            res.Info = new ResourceInfo()
            {
                Name = root.Attribute("name").Value,
                Description = root.Attribute("description").Value,
                Title = root.Attribute("title").Value,
                PluralTitle = root.Attribute("titlePlural").Value
            };
            res.Entries = new List<ResourceEntryInfo>();
            foreach (var item in root.Elements("resourceEntry"))
            {
                res.Entries.Add(new ResourceEntryInfo()
                {
                    // classId="Hello" translation="Hello, world" description="phrase: 'Hello, world'" date
                    ClassID = item.Attribute("classId").Value,
                    Translation = item.Attribute("translation").Value,
                    Description = item.Attribute("description").Value,
                    ModDate = item.Attribute("date").Value
                });                    
            }
            return res;            
        }

        #endregion
    }

    public static class ResFactory
    {
        public static IResourceReader GetReader()
        {
            return new XmlResourceReader("sampleResource.xml");
        }
    }
}
